/* Copyright 2018-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
 * applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package com.gitee.drinkjava2.gosqlgo;

import java.util.Random;

import com.gitee.drinkjava2.gosqlgo.compiler.CompileStrUtils;
import com.gitee.drinkjava2.gosqlgo.compiler.DynamicCompileEngine;

/**
 * ChildClassBuilder will create a new child class based on the parent class
 * 
 * @author Yong Zhu
 * @since 1.0.0
 */
public class ClassBuilder {
	private ClassBuilder() {
	}

	/**
	 * Build a random class name by give length
	 */
	public static String getRandomClassName(int length) {
		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuilder sb = new StringBuilder("C");
		for (int i = 0; i < length - 1; i++) {
			int number = random.nextInt(62);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * @param parentClass
	 *            optional, the parent class
	 * @param interFace
	 *            optional, the interface class
	 * @param packageName
	 *            the package name
	 * @param imports
	 *            the import packages
	 * @param body
	 *            The body of class
	 * @return the clild class created
	 */
	public static Class<?> build(Class<?> parentClass, Class<?> interFace, String packageName, String childClassName,
			String imports, String body) {
		String parentClassName = parentClass == null ? null : parentClass.getName();
		String interFaceName = interFace == null ? null : interFace.getName();
		return build(parentClassName, interFaceName, packageName, childClassName, imports, body);
	}

	/**
	 * @param parentClass
	 *            optional, the parent class full name
	 * @param interFace
	 *            optional, the interface class full name
	 * @param packageName
	 *            the package name
	 * @param imports
	 *            the import packages
	 * @param body
	 *            The body of class
	 * @return the clild class created
	 */
	public static Class<?> build(String parentClassName, String interfaceName, String packageName,
			String childClassName, String imports, String body) {
		String classfullName = childClassName;

		StringBuilder sb = new StringBuilder();
		if (!CompileStrUtils.isEmpty(packageName)) {
			sb.append("package ").append(packageName).append(";\r");
			classfullName = packageName + "." + childClassName;
		}
		if (!CompileStrUtils.isEmpty(imports))
			sb.append(imports);
		sb.append("public class ").append(childClassName);
		if (!CompileStrUtils.isEmpty(parentClassName))
			sb.append(" extends ").append(parentClassName);
		if (!CompileStrUtils.isEmpty(interfaceName))
			sb.append(" implements ").append(interfaceName);

		sb.append("{");
		sb.append(body);
		sb.append("\r}");
		String childClassSrc = sb.toString();
		Class<?> childClass = null;
		childClass = DynamicCompileEngine.instance.javaCodeToClass(classfullName, childClassSrc);
		return childClass;
	}

}
